package com.app.TCES.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.app.TCES.common.MemoryData;
import com.app.TCES.domain.Administrator;
import com.app.TCES.domain.Student;
import com.app.TCES.domain.Teacher;
import com.app.TCES.service.UserService;

@Controller
public class LoginController {

	Logger log = LogManager.getLogger(LoginController.class);

	@Autowired
	UserService userService;

	@RequestMapping("/Checklogin")
	@ResponseBody
	public Map<String, String> Login(HttpServletRequest request, HttpServletResponse response, int userId,
			String password, String select) {
//    	
//    	String password=request.getParameter("password");
//    	String role = request.getParameter("role");    	
		System.out.println("你输入的类型为=========》" + select);
		System.out.println("你输入的用户名为=========》" + userId);
		System.out.println("你输入的密码为===========》" + password);
		Map<String, String> map = new HashMap<String, String>();
		if (select.equals("系统管理员") == true) {

			Administrator administrator = new Administrator();
			administrator.setAdminId(userId);
			administrator.setPassword(password);
			Administrator administrator2 = userService.loginA(administrator);
			if (administrator2 == null) {
				map.put("msg", "error");
			} else {

				request.getSession().setAttribute("userName", administrator2.getAdminName());
				request.getSession().setAttribute("userId", administrator2.getAdminId());
				request.getSession().setAttribute("userRole", "admin");

				String sessionId = request.getRequestedSessionId();
				String user = administrator2.getAdminName();
				if (!MemoryData.getSessionIDMap().containsKey(user)) {
					MemoryData.getSessionIDMap().put(user, sessionId);

				} else if (MemoryData.getSessionIDMap().containsKey(user)
						&& StringUtils.equals(sessionId, MemoryData.getSessionIDMap().get(user))) {

					MemoryData.getSessionIDMap().remove(user);
					MemoryData.getSessionIDMap().put(user, sessionId);
				}
				map.put("msg", "管理员");
			}

		} else if (select.equals("教师") == true) {

			Teacher teacher = new Teacher();
			teacher.setTchId(userId);
			teacher.setPassword(password);
			Teacher teacher2 = userService.loginT(teacher);
			if (teacher2 == null) {
				map.put("msg", "error");

			} else {
				request.getSession().setAttribute("userName", teacher2.getTchName());
				request.getSession().setAttribute("userId", teacher2.getTchId());
				request.getSession().setAttribute("userRole", "teacher");

				String sessionId = request.getRequestedSessionId();
				String user = teacher2.getTchName();
				if (!MemoryData.getSessionIDMap().containsKey(user)) {

					MemoryData.getSessionIDMap().put(user, sessionId);
				} else if (MemoryData.getSessionIDMap().containsKey(user)
						&& StringUtils.equals(sessionId, MemoryData.getSessionIDMap().get(user))) {

					MemoryData.getSessionIDMap().remove(user);
					MemoryData.getSessionIDMap().put(user, sessionId);
				}
				map.put("msg", "教师");
			}

		} else if (select.equals("学生") == true) {

			Student student = new Student();
			student.setStuId(userId);
			student.setPassword(password);
			Student student2 = userService.loginS(student);
			if (student2 == null) {
				map.put("msg", "error");
			} else {
				request.getSession().setAttribute("userName", student2.getStuName());
				request.getSession().setAttribute("userId", student2.getStuId());
				request.getSession().setAttribute("userRole", "student");

				String sessionId = request.getRequestedSessionId();
				String user = student2.getStuName();
				if (!MemoryData.getSessionIDMap().containsKey(user)) {

					MemoryData.getSessionIDMap().put(user, sessionId);
				} else if (MemoryData.getSessionIDMap().containsKey(user)
						&& StringUtils.equals(sessionId, MemoryData.getSessionIDMap().get(user))) {

					MemoryData.getSessionIDMap().remove(user);
					MemoryData.getSessionIDMap().put(user, sessionId);
				}
				map.put("msg", "学生");
			}
		} else {
			map.put("msg", "错误");
		}
		return map;

	}

	@RequestMapping("/Logout")
	public String logout(HttpSession session, HttpServletRequest request) {
		
		log.debug(session.getAttribute("userName"));
		session.removeAttribute("userName");
		session.removeAttribute("userRole");
		return "/Login";
	}

	@RequestMapping("/Login")
	public String Login1(HttpSession session) {
		String role = (String)session.getAttribute("userRole");
		if (role != null) {
			if (role.equals("admin")) {
				return "/Admin_index";
			} else if (role.equals("teacher")) {
				return "/Teacher";

			} else if (role.equals("student")) {
				return "/Student";
			}
		}
		return "/Login";

	}

	@RequestMapping("/Student")
	public String Student() {

		return "/Student";
	}

	@RequestMapping("/Teacher")
	public String Teacher() {

		return "/Teacher";
	}

	@RequestMapping("/Administrator")
	public String Administrator() {

		return "/Administrator";
	}

}
